package Comprehensive_04;

public class Test4 {
    final Bank b;

    public Test4(Bank b) {
        this.b = b;
    }

    public static void main(String[] args) {
        Bank b = new Bank("ICC-1111", 1000);
        final Object o = new Object();
        Thread t1 = new Thread(() -> {
            try {
                synchronized (o) {
                    for (int i = 0; i < 10; i++) {
                        b.setMoney(b.getMoney() + 100);
                        System.out.println(Thread.currentThread().getName() + " 存入100元后，账户余额是：" + b.getMoney());
                        o.notify();
                        o.wait();
                    }
                    o.notify();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"小王");

        Thread t2 = new Thread(() -> {
            try {
                synchronized (o) {
                    for (int i = 0; i < 10; i++) {
                        b.setMoney(b.getMoney() + 100);
                        System.out.println(Thread.currentThread().getName() + " 存入100元后，账户余额是：" + b.getMoney());
                        o.notify();
                        o.wait();
                    }
                    o.notify();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"小李");

        t1.start();
        t2.start();

    }
}

class Bank {
    private String cardId;
    private int money;

    public void save(int money) {
        money += this.getMoney();
        this.setMoney(money);
    }

    public Bank() {
    }

    public Bank(String cardId, int money) {
        this.cardId = cardId;
        this.money = money;
    }

    /**
     * 获取
     *
     * @return cardId
     */
    public String getCardId() {
        return cardId;
    }

    /**
     * 设置
     *
     * @param cardId
     */
    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    /**
     * 获取
     *
     * @return money
     */
    public int getMoney() {
        return money;
    }

    /**
     * 设置
     *
     * @param money
     */
    public void setMoney(int money) {
        this.money = money;
    }

    public String toString() {
        return "Bank{cardId = " + cardId + ", money = " + money + "}";
    }
}


